package com.sdgakj.interviews.job20180814;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: Du Ronghong
 * @Description:
 * @Date: Created in 18:44 2018/8/14
 * @Modified By:
 */
public class ArrayTest {
    public static void main(String[] args) {
        int num[] = {1, 3, 4, 5, 6, 9, 10, 13, 14, 15, 16, 17, 18};
        System.out.println(Arrays.toString(getNum(num)));
    }

    private static int[] getNum(int[] num) {
        int len = num.length;
        if (len == 0) {
            return num;
        }
        Map<Integer, Integer> childArray = new LinkedHashMap<>(); // 存放当前连续数组
        Map<Integer, Integer> longArray = new LinkedHashMap<>();// 存放当前最长连续数组
        boolean serial = false;// 标记数据是否可连续
        for (int i = 0; i < len; i++) {
            int index = i + 1;
            if (index <= len - 1) {
                int m = num[index];
                int n = num[i];
                if (m - n == 1) {
                    serial = true;
                    childArray.put(n, 1);
                    childArray.put(m, 1);
                } else {
                    serial = false;
                }
            } else {
                serial = false;
            }
            // serial是false，标记当前连续已断开

            if (!serial) {
                // 通过当前连续数组和当前最长连续数组判断是否要替换

                if (childArray.size() > longArray.size()) {
                    longArray = childArray;
                }
                // 重置当前连续数组

                childArray = new LinkedHashMap<>();
            }
        }
        num = new int[longArray.keySet().size()];
        int i = 0;
        for (Integer m : longArray.keySet()) {
            num[i++] = m;
        }
        return num;
    }

}
